package com.dmwork.service.interaction.mq.callback;

import com.dmwork.common.base.result.ExceptionUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Data
@Component
public class ConfirmCallback {

    private String callbackName;

    public static final String COMMENT_CONFIRM = "commentConfirm";
    public static final String COMMENT_LIKE_CONFIRM = "commentLikeConfirm";
    public static final String FEED_CONFIRM = "feedConfirm";
    public static final String FEED_READ_CONFIRM = "feedReadConfirm";
    public static final String COMMENT_READ_CONFIRM = "commentReadConfirm";
    public static final String COMMENT_LIKE_READ_CONFIRM = "commentLikeReadConfirm";
    public static final String BATCH_FILE_REMOVE_CONFIRM = "batchFileRemoveConfirm";
    public static final String WORK_COLLECT_COUNT_CHANGE_CONFIRM="workCollectCountChangeConfirm";


    public final RabbitTemplate.ConfirmCallback originalConfirmCallback = new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            try {
                //使用反射，通过指定的方法名调用对应方法
                Method declaredMethod = ConfirmCallback.class.getDeclaredMethod(callbackName, CorrelationData.class, boolean.class, String.class);
                declaredMethod.invoke(ConfirmCallback.this, correlationData, ack, cause);
            } catch (Exception e) {
                ExceptionUtils.getMessage(e);
            }
        }
    };

    public void commentConfirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("-----------------comment confirmCallback--------------------");
        log.info("correlationData: " + correlationData);
        log.info("ack: " + ack);
        if (!ack) {
            log.info("处理异常");
        }
    }

    public void commentLikeConfirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("-----------------commentLike confirmCallback--------------------");
        log.info("correlationData: " + correlationData);
        log.info("ack: " + ack);
        if (!ack) {
            log.info("处理异常");
        }
    }

    public void feedConfirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("-----------------feed confirmCallback--------------------");
        log.info("correlationData: " + correlationData);
        log.info("ack: " + ack);
        if (!ack) {
            log.info("处理异常");
        }
    }

    public void feedReadConfirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("-----------------feedRead confirmCallback--------------------");
        log.info("correlationData: " + correlationData);
        log.info("ack: " + ack);
        if (!ack) {
            log.info("处理异常");
        }
    }

    public void commentReadConfirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("-----------------commentRead confirmCallback--------------------");
        log.info("correlationData: " + correlationData);
        log.info("ack: " + ack);
        if (!ack) {
            log.info("处理异常");
        }
    }

    public void commentLikeReadConfirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("-----------------commentLikeRead confirmCallback--------------------");
        log.info("correlationData: " + correlationData);
        log.info("ack: " + ack);
        if (!ack) {
            log.info("处理异常");
        }
    }

    public void batchFileRemoveConfirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("-----------------batchFileRemove confirmCallback--------------------");
        log.info("correlationData: " + correlationData);
        log.info("ack: " + ack);
        if (!ack) {
            log.info("处理异常");
        }
    }


    public void workCollectCountChangeConfirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("-----------------workCollectCountChange confirmCallback--------------------");
        log.info("correlationData: " + correlationData);
        log.info("ack: " + ack);
        if (!ack) {
            log.info("处理异常");
        }
    }

}
